<?php
namespace app\farm\model;

use app\BaseModel;
use app\shop\model\IntegralList;
use app\shop\model\SeckillList;
use think\facade\Db;

class Car extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_car';




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-10 15:19
     * @功能说明:获取购物车内的价格
     */
    public function carBreedPrice($user_id,$farmer_id){

        $dis = [

            'a.user_id'   => $user_id,

            'a.farmer_id' => $farmer_id,

            'a.type'      => 1,

            'a.status'    => 1
        ];

        $data = $this->alias('a')
                ->join('lbfarm_breed b','a.goods_id = b.id')
                ->where($dis)
                ->field('(a.goods_num*b.price) as total_price')
                ->select()
                ->toArray();

        return array_sum(array_column($data,'total_price'));

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-18 17:21
     * @功能说明:购物车价格
     */
    public function carPriceAndCount($user_id,$store_id,$is_car=0,$is_show=1,$no_i=1){
        //查询积分活动
        $integral_model = new IntegralList();

        $kill_model = new SeckillList();
        //选中
        $list = $this->carList($user_id,$store_id,0,$is_show);
        //所有 is_car0的时候也是选中
        $data['list'] = $this->carList($user_id,$store_id,$is_car,$is_show);
        //是否是秒杀商品
        $have_kill = $kill_model->getBuyLimit($data['list']);

        if(!empty($have_kill)){

            $buy_limit = 0;

            $no_i = 1;

            $discount_add = 0;

        }else{
            $min = $integral_model->getBuyLimit($data['list']);
            //获取是否允许原价购买 1允许 0不允许
            $buy_limit = $min['buy_limit'];

            $no_i = $buy_limit==1?$no_i:0;
            //是否允许优惠活动叠加
            $discount_add = $no_i==0?$min['discount_add']:1;

        }
        //积分抵扣掉的钱
        $integral_discount_price = $kill_discount_price = 0;
        //积分
        $integral = 0;

        if(!empty($data['list'])){

            foreach ($data['list'] as &$vs){

                $vs['member_discount'] = 100;

                $vs['integral']  = $vs['i_price'] = $vs['integral_id'] = $vs['kill_atv_id'] = $v['integral_discount_price'] = 0;

                $kill_list = $kill_model->atvIng($vs['goods_id'],$vs['spe_id']);
                //秒杀
                if(!empty($kill_list)){

                    $vs['true_price']  = $kill_list['price']*$vs['goods_num'];

                    $vs['kill_atv_id'] = $kill_list['id'];

                    $kill_discount_price = $vs['kill_discount_price'] = $vs['all_price'] - $vs['true_price'];

                }else{
                    //查询正在进行中的积分活动
                    $info = $integral_model->atvIng($vs['goods_id'],$vs['spe_id']);

                    if(!empty($info)){
                        //使用积分抵扣掉的钱
                        $integral_discount_price += $vs['all_price'] - $info['price']*$vs['goods_num'];
                        //使用积分
                        $integral += $info['integral']*$vs['goods_num'];
                        //使用积分抵扣
                        if($buy_limit==0||$no_i==0){

                            $vs['true_price']  = $info['price']*$vs['goods_num'];

                            $vs['integral_id'] = $info['atv_id'];

                        }
                    }
                }

                $vs['kill_price'] = !empty($kill_list)?$kill_list['price']:0;

                $vs['kill_end_time'] = !empty($kill_list)?$kill_list['end_time']:0;

                $vs['integral'] = !empty($info['integral'])?$info['integral']:0;

                $vs['i_price']  = !empty($info['price'])?$info['price']:0;

                $vs['total_integral_discount_price']  = !empty($info['price'])?$vs['all_price'] - $info['price']*$vs['goods_num']:0;

                $vs['total_integral'] = !empty($info['integral'])?$info['integral']*$vs['goods_num']:0;

            }

        }
        //总的数量
        $data['all_count'] = array_sum(array_column($data['list'],'goods_num'));

        if($is_car==0){

            $list = $data['list'];
        }
        //拼接订单
        $data['list'] = $this->getFarmerCar($data['list']);

        $data['kill_atv_id'] = !empty($kill_list)?$kill_list['id']:0;

        $data['kill_over_time'] = !empty($kill_list)?$kill_list['over_time']:0;
        //积分抵扣
        $data['integral'] = $integral;
        //积分抵扣多少钱
        $data['integral_discount_price'] = round($integral_discount_price,2);
        //
        $data['kill_discount_price'] = round($kill_discount_price,2);

        $data['buy_limit'] = $buy_limit;

        $data['discount_add'] = $discount_add;

        if(!empty($list)){

            $data['car_price']  = round(array_sum(array_column($list,'true_price')),2);
            //原价
            $data['init_price'] = round(array_sum(array_column($list,'all_price')),2);;
            //选中的数量
            $data['car_count']  = array_sum(array_column($list,'goods_num'));

        }else{

            $data['init_price'] = 0;

            $data['car_price']  = 0;

            $data['car_count']  = 0;

            $data['all_count']  = 0;

        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-23 11:26
     * @功能说明:转换成农场格式
     */
    public function getFarmerCar($car){

        $farmer_model = new Farmer();

        $arr = [];

        foreach ($car as $value){

            $arr[$value['farmer_id']]['goods_list'][] = $value;

            $arr[$value['farmer_id']]['farmer_id'] = $value['farmer_id'];

        }

        foreach ($arr as &$vs){

            $vs['farmer_info']= $farmer_model->dataInfo(['id'=>$vs['farmer_id']],'title,lng,lat,uniacid');

            $vs['car_price']  = round(array_sum(array_column($vs['goods_list'],'true_price')),2);
            //原价
            $vs['init_price'] = round(array_sum(array_column($vs['goods_list'],'all_price')),2);

            $vs['integral']   = round(array_sum(array_column($vs['goods_list'],'total_integral')),2);

            $vs['integral_discount_price'] = round(array_sum(array_column($vs['goods_list'],'total_integral_discount_price')),2);

            $vs['kill_discount_price'] = round(array_sum(array_column($vs['goods_list'],'kill_discount_price')),2);
            //运费
            $vs['freight']    = 0;

            $vs['coupon_discount'] = 0;

        }

        return array_values($arr);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-18 17:35
     * @功能说明:购物车列表
     */
    public function carList($user_id,$store_id=0,$all=0,$is_show=1){

        $dis = [

            'a.user_id' => $user_id,

            'b.status'  => 1,

            'a.type'    => 5,

            'a.is_show' => $is_show,

            'e.status'  => 2
        ];

        if($all==0){

            $dis['a.status'] = 1;
        }

        if(!empty($store_id)){

            $dis['d.store_id']= $store_id;
        }

        $data = $this->alias('a')
            ->join('lbfarm_shop_goods b','a.goods_id = b.id')
            ->join('lbfarm_v2_shop_spe_price c','a.spe_id = c.id','left')
            ->join('lbfarm_v2_goods_store d','a.goods_id = d.goods_id AND d.type=1')
            ->join('lbfarm_farmer e','d.store_id = e.id')
            ->where($dis)
            ->field('a.id,c.spe_id_1,b.send_tmpl_id,b.sale_num,b.true_sale_num,e.id as farmer_id,a.status,a.uniacid,a.goods_num,b.goods_name,b.cover,c.price,ROUND(c.price*a.goods_num,2) as all_price,ROUND(c.price*a.goods_num,2) as true_price,a.spe_id,a.goods_id,b.weight*a.goods_num as total_weight')
            ->group('a.id')
            ->select()
            ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['all_sale_num'] = $v['sale_num']+$v['true_sale_num'];

                $pec_id    = explode('-',$v['spe_id_1']);

                $spe_name  = Db::name('lbfarm_v2_shop_spe')->where(['status'=>1])->where('id','IN',$pec_id)->column('title');

                $v['spe_name'] = implode('-',$spe_name);

            }

        }

        return $data;


    }





}